// src/shared/api/user.ts

import { ApiResponse } from '../types';
import {
  getProfile as getProfileService,
  updateProfile as updateProfileService,
  getAddresses as getAddressesService,
  addAddress as addAddressService,
  deleteAddress as deleteAddressService,
  updateUserPermission as updateUserPermissionService,
  getAllUsers as getAllUsersService,
  UpdateProfileRequest,
  ProfileResponse,
  AddAddressRequest,
  AddressResponse,
  AddressListResponse,
  UpdateUserPermissionRequest,
  UserPermissionResponse,
  UserListResponse
} from '../service/userService';

/**
 * Mock API: 获取用户信息
 * GET /api/user/profile
 */
export const GET_profile = async (): Promise<ApiResponse<ProfileResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 200));

    // 调用Service层
    const result = await getProfileService();

    return result;
  } catch (error) {
    console.error('Get Profile API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 更新用户信息
 * PUT /api/user/profile
 */
export const PUT_profile = async (body: UpdateProfileRequest): Promise<ApiResponse<ProfileResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await updateProfileService(body);

    return result;
  } catch (error) {
    console.error('Update Profile API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 获取用户地址列表
 * GET /api/user/addresses
 */
export const GET_addresses = async (): Promise<ApiResponse<AddressListResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await getAddressesService();

    return result;
  } catch (error) {
    console.error('Get Addresses API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 添加新地址
 * POST /api/user/addresses
 */
export const POST_address = async (body: AddAddressRequest): Promise<ApiResponse<AddressResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 调用Service层
    const result = await addAddressService(body);

    return result;
  } catch (error) {
    console.error('Add Address API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 删除地址
 * DELETE /api/user/addresses/:id
 */
export const DELETE_address = async (addressId: string): Promise<ApiResponse<null>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 300));

    // 调用Service层
    const result = await deleteAddressService(addressId);

    return result;
  } catch (error) {
    console.error('Delete Address API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};


// ===== 管理员用户管理API =====

/**
 * Mock API: 获取所有用户列表（管理员功能）
 * GET /api/user/admin/users
 */
export const GET_admin_users = async (): Promise<ApiResponse<UserListResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 400));

    // 调用Service层
    const result = await getAllUsersService();

    return result;
  } catch (error) {
    console.error('Get Admin Users API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};

/**
 * Mock API: 更新用户权限（管理员功能）
 * PUT /api/user/admin/permission
 */
export const PUT_admin_permission = async (body: UpdateUserPermissionRequest): Promise<ApiResponse<UserPermissionResponse>> => {
  try {
    // 模拟网络延迟
    await new Promise(resolve => setTimeout(resolve, 500));

    // 调用Service层
    const result = await updateUserPermissionService(body);

    return result;
  } catch (error) {
    console.error('Update User Permission API Error:', error);
    return {
      code: 500,
      message: '网络请求失败',
      data: null
    };
  }
};